package com.pku.smart.controller;

import com.alibaba.fastjson.JSON;
import com.pku.smart.constant.PayConstant;
import com.pku.smart.log.MyLog;
import com.pku.smart.service.SmartPayService;
import com.pku.smart.trade.service.IPaymentManagerService;
import com.pku.smart.trade.vopackage.VoReqPayCode;
import com.pku.smart.trade.vopackage.VoResPayCode;
import com.pku.smart.trade.vopackage.VoResTradePayment;
import com.pku.smart.vopackage.TradeResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 条码支付 刷卡支付
 * 商户通过扫码设备扫描用户手机
 */
@RestController
@RequestMapping(value = "/api")
public class PayCodeController {

    private static final MyLog _log = MyLog.getLog(PayCodeController.class);

    @Autowired
    SmartPayService smartPayService;

    @Autowired
    IPaymentManagerService paymentManagerService;

    /**
     * 条码支付
     *
     * @param requestVo     mchId channelId mchOrderNo totalAmount subject body authCode scene clientIp device extra sign
     * @param bindingResult 校验必填字段 mchId channelId mchOrderNo totalAmount subject authCode clientIp sign
     * @return 统一返回retCode errCode errCodeDes  支付成功：加mchId mchOrderNo channelOrderNo
     */
    @RequestMapping("/pay/pay_order")
    public String codePay(@ModelAttribute VoReqPayCode requestVo, BindingResult bindingResult) {
        TradeResultVo tradeResultVo = new TradeResultVo();

        _log.info("###### 开始接收商户统一条码支付请求 ######");
        try {
            //参数校验
            smartPayService.validateParams(requestVo, bindingResult);
            //调用服务
            VoResTradePayment payResultVo = paymentManagerService.codePay(requestVo);
            //结果转换
            tradeResultVo.setRetCode(payResultVo.getRetCode());
            //默认值
            VoResPayCode resPayCode = (VoResPayCode) payResultVo.getRetData();
            tradeResultVo.setErrCode(resPayCode.getErrCode());
            tradeResultVo.setErrCodeDes(resPayCode.getErrCodeDes());
            tradeResultVo.setInvokeStatus(resPayCode.getInvokeStatus());
            //支付不是直接返回成功
            if (PayConstant.TRADE_STATUS_SUCCESS.equalsIgnoreCase(payResultVo.getRetCode())) {
                tradeResultVo.setMchId(resPayCode.getMchId());
                tradeResultVo.setMchOrderNo(resPayCode.getMchOrderNo());
                tradeResultVo.setChannelOrderNo(resPayCode.getChannelOrderNo());
            }
        } catch (Exception e) {
            e.printStackTrace();
            tradeResultVo.setRetCode(PayConstant.TRADE_STATUS_FAILED);
            tradeResultVo.setErrCode(PayConstant.TRADE_STATUS_SYSTEM_ERROR_CODE);
            tradeResultVo.setErrCodeDes("系统异常：" + e.getMessage());
            _log.error(PayConstant.TRADE_STATUS_SYSTEM_ERROR_DESC, e);
        }

        String result = JSON.toJSONString(tradeResultVo);
        _log.info("打印结果：" + result);

        return result;
    }

}
